package com.smart.six.locale;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 测试本地化
 *
 * @author junrui.wang
 * @create 2018/7/13
 */
public class LocaleTest {

    public static void testNumberFormat(Locale locale, double price){
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
        System.out.println(numberFormat.format(price));
    }

    public static void testDateFormat(Locale locale, Date date){
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
        System.out.println(dateFormat.format(date));
    }

    public static void testMessageFormat(){
        //格式化信息串
        String pattern1 = "{0}, 您好! 您于{1}在工商银行存入{2}元.";
        String pattern2 = "At {1, time, short} On {1, date, long}, {0} paid {2, number, currency}";

        //用于动态替换占位符的参数
        Object[] params = {"John", new GregorianCalendar().getTime(), 1.0E3};

        //使用默认本地化对象格式化信息1
        String s1 = MessageFormat.format(pattern1, params);

        //使用指定本地化对象格式化信息2
        MessageFormat messageFormat = new MessageFormat(pattern2, Locale.US);
        String s2 = messageFormat.format(params);
        System.out.println(s1);
        System.out.println(s2);
    }

    public static void testLocale(){
        //带有语言和国家/地区信息的本地化信息
        Locale locale = new Locale("zh", "CN");

        //只有语言的本地化信息
        Locale locale1 = new Locale("zh");

        //等同于Locale locale = new Locale("zh", "CN");
        Locale locale2 = Locale.CHINA;

        //等同于Locale locale1 = new Locale("zh");
        Locale locale3 = Locale.CHINESE;

        //获取本地系统默认的本地化对象
        Locale locale4 = Locale.getDefault();
    }



    public static void main(String[] args) {

    }
}
